package com.kaili.basic.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.kaili.basic.mapper.BaseMoveStoreMapper;
import com.kaili.basic.mapper.BaseReportLossMapper;
import com.kaili.basic.pojo.dto.*;
import com.kaili.basic.pojo.entity.BaseMoveStoreDetailEntity;
import com.kaili.basic.pojo.entity.BaseMoveStoreEntity;
import com.kaili.basic.pojo.entity.BaseReportLossDetailEntity;
import com.kaili.basic.pojo.entity.BaseReportLossEntity;
import com.kaili.basic.pojo.vo.BaseMoveStoreExportVO;
import com.kaili.basic.pojo.vo.BaseMoveStoreVO;
import com.kaili.basic.pojo.vo.BaseReportLossDetailVO;
import com.kaili.basic.service.*;
import com.kaili.common.exception.RestException;
import com.kaili.common.response.CommonStatus;
import com.kaili.common.response.RestStatus;
import com.kaili.common.util.LoginDetailUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
@Transactional
public class BaseReportLossServiceImpl extends ServiceImpl<BaseReportLossMapper, BaseReportLossEntity> implements BaseReportLossService {
    @Autowired
    private BaseReportLossMapper baseReportLossMapper;

    @Autowired
    private BaseReportLossDetailService baseReportLossDetailService;

    @Autowired
    private BaseStoreStockService baseStoreStockService;

    @Autowired
    private BaseStoreAccountsService baseStoreAccountsService;

    @Override
    public Long queryStoreOrderNo() {
        return baseReportLossMapper.queryStoreOrderNo();
    }

    @Override
    public RestStatus addReportLoss(BaseReportLossAddDTO dto) {
        //新增报损记录
        BaseReportLossEntity entity=new BaseReportLossEntity();
        BeanUtils.copyProperties(dto,entity);
        entity.setCreateBy(LoginDetailUtils.getCurrentEmployeeId().longValue());
        entity.setCreateTime(new Date());
        //统计数量
        for (BaseReportLossAddDetailDTO detail:dto.getDetailList()) {
            entity.setTotalTrays(entity.getTotalTrays()+detail.getTrays());
            entity.setTotalVo(entity.getTotalVo()+detail.getVolume());
            entity.setTotalWeight(entity.getTotalWeight()+detail.getWeight());
            entity.setAmount(entity.getAmount()+detail.getAmount());
        }
        this.insert(entity);
        //新增报损明细
        baseReportLossDetailService.insertBatch(dto.getDetailList(),entity.getOrderNo());
        return CommonStatus.ADD_OK;
    }

    @Override
    public RestStatus updateReportLoss(BaseReportLossEditDTO dto) {
        //新增报损记录
        BaseReportLossEntity entity=new BaseReportLossEntity();
        BeanUtils.copyProperties(dto,entity);
        entity.setModifyBy(LoginDetailUtils.getCurrentEmployeeId().longValue());
        entity.setModifyTime(new Date());
        //统计数量
        for (BaseReportLossAddDetailDTO detail:dto.getDetailList()) {
            entity.setTotalTrays(entity.getTotalTrays()+detail.getTrays());
            entity.setTotalVo(entity.getTotalVo()+detail.getVolume());
            entity.setTotalWeight(entity.getTotalWeight()+detail.getWeight());
            entity.setAmount(entity.getAmount()+detail.getAmount());
        }
        this.updateById(entity);

        //删除原始库存
        baseReportLossDetailService.deleteByMap(new HashMap<String, Object>() {{
            put("order_no", entity.getOrderNo());
        }});
        //新增修改后的明细
        baseReportLossDetailService.insertBatch(dto.getDetailList(),entity.getOrderNo());
        return CommonStatus.UPDATE_OK;
    }

    @Override
    public RestStatus auditReportLoss(BaseReportLossAuditDTO auditDto) throws RuntimeException{
        BaseReportLossEntity entity = this.selectById(auditDto.getId());
        //已经做过审核不做处理
        if(!("0".equals(entity.getIsAuduit()))){
            return CommonStatus.UPDATE_OK;
        }
        //1.更改移库单为已审核
        entity.setIsAuduit(auditDto.getIsAudit());
        entity.setAuditBy(LoginDetailUtils.getCurrentEmployeeId().longValue());
        entity.setCreateTime(new Date());
        this.updateById(entity);

        //审核通过做正式移库操作
        List<BaseReportLossDetailEntity> detailList= baseReportLossDetailService.selectByMap(new HashMap<String, Object>() {{
            put("order_no", auditDto.getOrderNo());
        }});

        //2.更新原库存 如果存在库存不足则抛出异常
        Boolean flage = baseStoreStockService.updateStock(detailList);
        if(!flage){
            throw new RuntimeException();
        }
        //3.批量新增新库存
        baseStoreStockService.bathInsert(detailList,this.selectById(entity.getId()).getSupplierId());

        //生成台账记录
        baseStoreAccountsService.insertBath(detailList);
        return CommonStatus.UPDATE_OK;
    }

    @Override
    public Page<BaseReportLossDetailVO> queryPageList(Page<BaseReportLossDetailVO> page, BaseReportLossQueryDto queryDto) {
        List<BaseReportLossDetailVO> list = baseReportLossMapper.queryPageList(page, queryDto);
        page.setRecords(list);
        return page;
    }

    @Override
    public List<BaseReportLossDetailVO> queryDetailByOrderNo(HashMap<String, Object> map) {
        return baseReportLossMapper.queryDetailByOrderNo(map);
    }

    @Override
    public BaseReportLossDetailVO queryReportLossDetail(HashMap<String, Object> map) {
        BaseReportLossDetailVO lossDetailVO = baseReportLossMapper.queryPageList(map);
        List<BaseReportLossDetailVO> list = baseReportLossMapper.queryDetailByOrderNo(new HashMap<String, Object>() {{
            put("orderNo", lossDetailVO.getOrderNo());
        }});
        lossDetailVO.setList(list);
        return lossDetailVO;
    }

    @Override
    public List<BaseMoveStoreExportVO> export(BaseReportLossQueryDto queryDto) {
        return baseReportLossMapper.reportLossExport(queryDto);
    }
}
